from models import execute


async def get_cart_all_sku(sku_id_list):
    _, skus = await execute('select * from good_sku where id in (%s)' % ','.join(['%s'] * len(sku_id_list)),
                            tuple(sku_id_list))
    return skus


async def get_property_value_from_sku_id(sku):
    _, property_values = await execute('select title from property_value where sku_id = %s', (sku.get('id')))
    return property_values


async def query_goods_is_exist(spu_id):
    if not isinstance(spu_id, list):
        _, res = await execute('select id from good where id  = %s', (spu_id,))
        return res
    else:
        _, res = await execute('select id from good where id in (%s)' % ','.join(['%s'] * len(spu_id)), tuple(spu_id))
        return res


async def query_sku_from_spu(spu_id):
    _, sku_ids = await execute('select id from good_sku where good_id = %s', (spu_id,))
    return sku_ids


async def query_spu_property_count(sku_id_count, new_sku_ids):
    _, res = await execute(
        'select count(distinct property_id) as total from property_value where sku_id in (%s);' % ','.join(
            ['%s'] * sku_id_count), tuple(new_sku_ids))
    return res


async def query_sku_from_property_title_skus(sku_id_count, new_sku_ids):
    _, res = await execute('select sku_id from property_value where sku_id in (%s)' % ','.join(
        ['%s'] * sku_id_count) + 'and title = %s', tuple(new_sku_ids))
    return res


async def query_sku_is_exist(sku_id):
    if not isinstance(sku_id, list):
        _, sku = await execute('select * from good_sku where id = %s', (sku_id, ))
    else:
        _, sku = await execute('select id from good_sku where id in (%s)' % ','.join(['%s'] * len(sku_id)),
                               tuple(sku_id))
    return sku
